package com.example.sample.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.util.MPermissionDelegate;

import net.liteheaven.xblib.common.ui.ToastUtil;

import androidx.annotation.NonNull;

/**
 * Created by LiangYu on 2018/4/9.
 */
public class PermissionActivity extends BaseActivity{

    private static final String [] requiredPermissions = new String [] {
            Manifest.permission.CALL_PHONE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.RECEIVE_SMS,
            Manifest.permission.CAMERA,
            Manifest.permission.GET_ACCOUNTS,
            Manifest.permission.SEND_SMS,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };

    private MPermissionDelegate.PermissionResultCallback callback = new MPermissionDelegate.PermissionResultCallback() {
        @Override
        public void onSuccess() {
            //call();
        }

        @Override
        public void onFailure(boolean isBan) {
            Activity context = PermissionActivity.this;
            ToastUtil.show(context, "没权限");
        }
    };
    private MPermissionDelegate delegate = new MPermissionDelegate(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        delegate.checkPermissions(requiredPermissions, callback);
        setContentView(R.layout.activity_mpermission);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        delegate.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void dial(){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + "10086");
        intent.setData(data);
        startActivity(intent);
    }

    private void call(){
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "10086");
        intent.setData(data);
        startActivity(intent);
    }
}
